<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/19
 * Time: 18:54
 */

namespace app\admin\logic;


use app\admin\model\goods\Goods;
use think\Model;

class GoodsLogic extends Model
{
    /**
     * 获取 规格的 笛卡尔积
     * @param $goods_id 商品 id
     * @param $spec_arr 笛卡尔积
     * @return string 返回表格字符串
     */
    public function getSpecInput($goods_id, $spec_arr)
    {
        // 排序
        foreach ($spec_arr as $k => $v)
        {
            $spec_arr_sort[$k] = count($v);
        }
        asort($spec_arr_sort);
        foreach ($spec_arr_sort as $key =>$val)
        {
            $spec_arr2[$key] = $spec_arr[$key];
        }


        $clo_name = array_keys($spec_arr2);

        $spec_arr2 = combineDika($spec_arr2); //  获取 规格的 笛卡尔积

//        $goods = Goods::get($goods_id);

        $spec = model('GoodsSpecGroup','model\goods')
            ->column('spec_group_id,spec_name'); // 规格表
        $specItem = model('GoodsSpecItem','model\goods')
            ->column('spec_item_id,item_name,spec_group_id');//规格项
        $keySpecGoodsPrice = model('GoodsSpec','model\goods')
            ->where('goods_id', '=',$goods_id)
            ->column('goods_spec_id,goods_spec_sn,spec_item_key,spec_item_value,spec_price,spec_stock,spec_image,unit,weight,status,extra','spec_item_key');//规格项

        foreach ($keySpecGoodsPrice as $key => $val){
            if(isset($keySpecGoodsPrice[$key]['extra']) && !empty($keySpecGoodsPrice[$key]['extra'])){
                $keySpecGoodsPrice[$key]['extra'] = json_decode($keySpecGoodsPrice[$key]['extra'], true);
            }
        }

//        dump($keySpecGoodsPrice);die;

        $str = "<table width='100%' class='table table-striped table-bordered table-hover table-nowrap' id='spec_input_tab'>";
        $str .="<tr>";
        // 显示第一行的数据
        foreach ($clo_name as $k => $v)
        {
            if($v == 0){
                return $str;
            }
            $str .=" <td><b>{$spec[$v]}</b></td>";
        }
        $str .="<td><b>价格</b></td>";
        $str .="<td><b>库存</b></td>
                <td><b>单位</b></td>
                <td><b>重量(kg)</b></td>
                <td><b>编码</b></td>
                <td style='width: 100%'><b>图片</b></td>
               <td><b>操作</b></td>
             </tr>";
        // 显示第二行开始
        foreach ($spec_arr2 as $k => $v)
        {
            $str .="<tr>";
            $item_key_name = array();
            foreach($v as $k2 => $v2)
            {
                $str .="<td>{$specItem[$v2]['item_name']}</td>";
                $item_key_name[$v2] = $spec[$specItem[$v2]['spec_group_id']].':'.$specItem[$v2]['item_name'];
            }
            ksort($item_key_name);

            $item_key = implode(',', array_keys($item_key_name));
            if(!array_key_exists($item_key,$keySpecGoodsPrice)) {
                $keySpecGoodsPrice[$item_key]["goods_spec_id"] = 0;
                $keySpecGoodsPrice[$item_key]["spec_price"] = 0.00;
                $keySpecGoodsPrice[$item_key]["spec_stock"] = 0;
                $keySpecGoodsPrice[$item_key]["goods_spec_sn"] = '';
                $keySpecGoodsPrice[$item_key]["unit"] = '';
                $keySpecGoodsPrice[$item_key]["weight"] = 0.000;
                $keySpecGoodsPrice[$item_key]["spec_image"] = '';
                $keySpecGoodsPrice[$item_key]["status"] = 1;
            }
            $str .= "<td><input style='width: 80px' type='number' name='row[item][$item_key][spec_price]' value='{$keySpecGoodsPrice[$item_key]["spec_price"]}' onkeyup='this.value=this.value.replace(/[^\d.]/g,\"\")' onpaste='this.value=this.value.replace(/[^\d.]/g,\"\")' /></td>";

            $str .="<td><input style='width: 80px' type='number' name='row[item][$item_key][spec_stock]' value='{$keySpecGoodsPrice[$item_key]["spec_stock"]}' onkeyup='this.value=this.value.replace(/[^\d]/g,\"\")' onpaste='this.value=this.value.replace(/[^\d]/g,\"\")' /></td>";
            $str .="<td><input style='width: 50px' type='text' name='row[item][$item_key][unit]' value='{$keySpecGoodsPrice[$item_key]["unit"]}'/></td>";
            $str .="<td><input style='width: 50px' type='text' name='row[item][$item_key][weight]' value='{$keySpecGoodsPrice[$item_key]["weight"]}'/></td>";
            $str .="<td><input style='width: 120px' type='text' name='row[item][$item_key][goods_spec_sn]' value='{$keySpecGoodsPrice[$item_key]["goods_spec_sn"]}'/></td>";
            $str .="<td><input id='c-spec_image-{$k}' type='hidden' name='row[item][$item_key][spec_image]' value='{$keySpecGoodsPrice[$item_key]["spec_image"]}' />";
            $str .="<span>";
            $str .="<button type='button' id='plupload-spec_image-{$k}' class='btn btn-danger plupload list-block' data-input-id='c-spec_image-{$k}' data-mimetype='image/gif,image/jpeg,image/png,image/jpg,image/bmp' data-multiple='false' data-preview-id='p-spec_image-{$k}'><i class='fa fa-upload'></i> 上传</button>";
            $str .="<button type='button' id='fachoose-spec_image-{$k}' class='btn btn-primary fachoose' data-input-id='c-spec_image-{$k}' data-preview-id='p-spec_image-{$k}' data-mimetype='image/*' data-multiple='false'><i class='fa fa-list'></i> 选择</button>";
            $str .="</span>";
            $str .="<input type='hidden' name='row[item][$item_key][goods_spec_id]' value='{$keySpecGoodsPrice[$item_key]["goods_spec_id"]}' />";
            $str .="<ul class='row list-inline plupload-preview p-spec_image' style='' id='p-spec_image-{$k}' ></ul></td>";

            if($keySpecGoodsPrice[$item_key]["status"] == 1){
                $str .="<td><button type='button' class='btn btn-primary btn-danger delete_item'>禁用</button><input type='hidden' name='row[item][$item_key][status]' value='{$keySpecGoodsPrice[$item_key]["status"]}'></td>";
            }else{
                $str .="<td><button type='button' class='btn btn-primary btn-info delete_item'>启用</button><input type='hidden' name='row[item][$item_key][status]' value='{$keySpecGoodsPrice[$item_key]["status"]}'></td>";
            }

            $str .="</tr>";
        }
        $str .= "</table>";
        return $str;
    }
}